import { requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { useCallback, useMemo, useState } from 'react';
import { PlanManageAPI } from '../common/type';
// import { ManageSearchColumns } from '../mconfig';
// import { disposeArea } from '../common';
import { ManageTabSwitch } from '../mconfig';
import { getBaseUrl } from '@/services/urlconst';

const baseUrl = getBaseUrl();

const PlanMaterialInfoServices = (curRes: Record<string, any>) => {
  /**
   * 保存当前的搜索参数
   * */
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /**
   * 管理页获取数据方法
   * */
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: PlanManageAPI.IAssetTableRequestType) => {
      let PostParams = {
        // current: 1,
        // pageSize: 10,
        page: 1,
        rows: 10,
        ...reqParams,
        ...ManageTabSwitch(curRes),
      };

      let reqUrl = `${baseUrl}/purchaseNeedMaterialController/dataGrid/${PostParams?.params}`;

      /***保存一份参数**/
      setManageTableParams(PostParams);

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 重新请求的方法
   * */
  const reloadRequest = useCallback((reqParams: PlanManageAPI.IAssetTableRequestType | any) => {
    run({ ...reqParams });
  }, []);

  /**
   * 页面数据及分页信息
   * */
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    dataRow: data,
    loading,
    reloadRequest,
    manageTableParams,
    ...tableValues, //页面列表值
  };
};

export { PlanMaterialInfoServices };
